package com.foreknow.elm.dao.Impl;

import com.foreknow.elm.dao.CartDao;
import com.foreknow.elm.po.Business;
import com.foreknow.elm.po.Cart;
import com.foreknow.elm.po.Food;
import com.foreknow.elm.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CartDaoImpl implements CartDao {
    private Connection conn;
    private PreparedStatement pst;
    private ResultSet rs;

    @Override
    public List<Cart> listCart(String userId, Integer businessId) throws SQLException {
        List<Cart> list = new ArrayList<>();
        StringBuffer sql = new StringBuffer("SELECT * FROM cart c JOIN food f on f.foodId=c.foodId join user u on c.userId=u.userId and c.userId=?");
        conn = DBUtil.getConnection();
        try {
            if (businessId != 0) {
                sql.append(" join business b on c.businessId=b.businessId and c.businessId=?");
            }
            pst = conn.prepareStatement(String.valueOf(sql));
            pst.setString(1, userId);
            if (businessId != 0) {
                pst.setInt(2, businessId);
            }
            rs = pst.executeQuery();
            while (rs.next()) {
                Cart cart = new Cart();
                Food food = new Food();
                food.setFoodId(rs.getInt("FoodId"));
                food.setFoodName(rs.getString("FoodName"));
                food.setFoodExplain(rs.getString("FoodExplain"));
                food.setFoodImg(rs.getString("FoodImg"));
                food.setFoodPrice(rs.getDouble("FoodPrice"));
                food.setBusinessId(rs.getInt("BusinessId"));
                food.setRemarks(rs.getString("Remarks"));
                if (businessId!=0) {
                    Business bu = new Business();
                    bu.setBusinessId(rs.getInt("BusinessId"));
                    bu.setBusinessName(rs.getString("BusinessName"));
                    bu.setBusinessAddress(rs.getString("BusinessAddress"));
                    bu.setBusinessExplain(rs.getString("BusinessExplain"));
                    bu.setBusinessImg(rs.getString("BusinessImg"));
                    bu.setOrderTypeId(rs.getInt("OrderTypeId"));
                    bu.setStarPrice(rs.getDouble("StarPrice"));
                    bu.setDeliveryPrice(rs.getDouble("DeliveryPrice"));
                    bu.setRemarks(rs.getString("Remarks"));
                    cart.setBusiness(bu);
                }
                cart.setCartId(rs.getInt("CartId"));
                cart.setFoodId(rs.getInt("FoodId"));
                cart.setBusinessId(rs.getInt("BusinessId"));
                cart.setUserId(rs.getString("UserId"));
                cart.setQuantity(rs.getInt("Quantity"));
                cart.setFood(food);
                list.add(cart);
            }
        } finally {
            DBUtil.close(pst);
        }
        return list;
    }

    @Override
    public int saveCart(String userId, Integer businessId, Integer foodId) throws SQLException {
        int i = 0;
        String sql = "INSERT into cart (foodId,businessId,userId,quantity) VALUES (?,?,?,1)";
        try {
            conn = DBUtil.getConnection();
            pst = conn.prepareStatement(sql);
            pst.setInt(1, foodId);
            pst.setInt(2, businessId);
            pst.setString(3, userId);
            i = pst.executeUpdate();
        } finally {
            DBUtil.close(pst);
        }
        return i;
    }

    @Override
    public int updateCart(String userId, Integer businessId, Integer foodId, Integer quantity) throws SQLException {
        int i = 0;
        String sql = "UPDATE cart set quantity=? where foodId=? and businessId=? and userId=?";
        try {
            conn = DBUtil.getConnection();
            pst = conn.prepareStatement(sql);
            pst.setInt(1, quantity);
            pst.setInt(2, foodId);
            pst.setInt(3, businessId);
            pst.setString(4, userId);
            i = pst.executeUpdate();
        } finally {
            DBUtil.close(pst);
        }
        return i;
    }

    @Override
    public int removeCart(String userId, Integer businessId, Integer foodId) throws SQLException {
        int i = 0;
        StringBuffer sql = new StringBuffer("DELETE FROM cart where userId=? and businessId=?");
        if (foodId != 0) {
            sql.append(" and foodId=?");
        }
        try {
            conn = DBUtil.getConnection();
            pst = conn.prepareStatement(String.valueOf(sql));
            pst.setString(1, userId);
            pst.setInt(2, businessId);
            if (foodId != 0) {
                pst.setInt(3, foodId);
            }
            i = pst.executeUpdate();
        } finally {
            DBUtil.close(pst);
        }
        return i;
    }

    public static void main(String[] args) throws SQLException {
        CartDao cd = new CartDaoImpl();

//        System.out.println(cd.removeCart("3333333333",10002,5));

//        System.out.println(cd.updateCart("3333333333",10003,7,20));

//        int i=cd.saveCart("3333333333",10002,5);
//        System.out.println(i);

        List<Cart> list = cd.listCart("11111111111", 10003);
//        for (int i = 0; i < list.size(); i++) {
//            if (list.get(i).getBusiness()==null){
//                System.out.println(list.get(i).getCartId() + "---" + list.get(i).getUserId() + "---" + list.get(i).getFood().getFoodId() + "---" + list.get(i).getFood().getFoodName()+list.get(i).getBusinessId());
//            }else{
//                System.out.println(list.get(i).getCartId() + "---" + list.get(i).getUserId() + "---" + list.get(i).getBusinessId() + list.get(i).getBusiness().getBusinessName()+ "---" + list.get(i).getFood().getFoodId() + "---" + list.get(i).getFood().getFoodName());
//            }
//        }
    }
}

